package com.example.wuqujingdianphone.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.activity.BookDetailActivity;
import com.example.wuqujingdianphone.activity.BookWebDetailActivity;
import com.example.wuqujingdianphone.adapter.BCMultiMediaAdapter;
import com.example.wuqujingdianphone.adapter.BRBookAdapter;
import com.example.wuqujingdianphone.api.GlobalConfig;
import com.example.wuqujingdianphone.base.BaseFragment1;
import com.example.wuqujingdianphone.bean.BookListBean;
import com.example.wuqujingdianphone.bean.MultiMediaBean;
import com.example.wuqujingdianphone.utils.NiceDialogUtils;
import com.example.wuqujingdianphone.utils.SPUtil;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.vise.xsnow.http.ViseHttp;
import com.vise.xsnow.http.callback.ACallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by yuanpk on 2018/4/9.
 * 书架---听书
 */

public class BRListenBookFragment extends BaseFragment1 {
    private static final String TAG = "BRListenBookFragment";

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    Unbinder unbinder;

    private BCMultiMediaAdapter mAdapter;

    public static BRListenBookFragment getBRListenBookFragment() {
        BRListenBookFragment fragment = new BRListenBookFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_book_rack_listen_book, null);
        unbinder = ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
        mAdapter = new BCMultiMediaAdapter(getActivity(), null);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);


        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Log.i(TAG, "onItemClick: position=" + position);
                Intent intent = new Intent(getActivity(), BookWebDetailActivity.class);
                intent.putExtra("type", "听书");
                intent.putExtra("bookId", mAdapter.getItem(position).id);
                startActivity(intent);


            }
        });


    }

    private void initData() {
        // NiceDialogUtils.showNiceDialog(getChildFragmentManager());

        //http://wenqujingdian.com/index.php/Jiekou/yinpin/
        //String account = SPUtil.getString(getActivity(), GlobalConfig.ACCOUNT_KEY, "");
        //Log.i(TAG, "initData: account=" + account);
        ViseHttp.GET("index.php/Jiekou/yinpin/")//account
                .tag(TAG)

                .request(new ACallback<List<MultiMediaBean>>() {
                    @Override
                    public void onSuccess(List<MultiMediaBean> bookListBeen) {
                        cancelLoading();
                        //请求成功，AuthorModel为解析服务器返回数据的对象，可以是String，按需定义即可

                        if (bookListBeen != null && bookListBeen.size() > 0) {
                            Log.i(TAG, "onSuccess: bookListBeen.size()=" + bookListBeen.size());
                            mAdapter.setNewData(bookListBeen);
                        } else {
                            mAdapter.setNewData(null);
                            mAdapter.setEmptyView(R.layout.comment_empty_view, (ViewGroup) mRecyclerView.getParent());
                        }
                    }

                    @Override
                    public void onFail(int errCode, String errMsg) {
                        cancelLoading();
                        //请求失败，errCode为错误码，errMsg为错误描述
                        ToastUtil.showToast("errCode=" + errCode + " errMsg=" + errMsg);
                    }
                });


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}




